package com.ruoyi.resumes.service.impl;

import java.io.*;
import java.util.*;


import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import com.ruoyi.work.domain.Jobbasicinfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.resumes.mapper.ResumesMapper;
import com.ruoyi.resumes.domain.Resumes;
import com.ruoyi.resumes.service.IResumesService;


/**
 * 简历管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-02
 */
@Service
public class ResumesServiceImpl implements IResumesService
{
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);
    @Autowired
    private ResumesMapper resumesMapper;

    /**
     * 查询简历管理
     *
     * @param id 简历管理主键
     * @return 简历管理
     */
    @Override
    public Resumes selectResumesById(Long id)
    {
        return resumesMapper.selectResumesById(id);
    }

    /**
     * 查询简历管理列表
     *
     * @param resumes 简历管理
     * @return 简历管理
     */
    @Override
    public List<Resumes> selectResumesList(Resumes resumes)
    {
        //获取当前用户id
        Long userId=SecurityUtils.getUserId();
        //获取role_id
        List roleId=resumesMapper.getRoleId(userId);
        //判断是否是应聘者
        if (roleId.contains(100)){
            List<Resumes> resumesList=resumesMapper.selectResumesByUserId(userId);
            //大字段显示
            List<Resumes> newResumesList=stringSub(resumesList);
            List<Resumes> resumes1=resumesMapper.selectResumesList(resumes);
            List<Resumes> resumes2=stringSub(resumes1);
            //两个集合取交集
            Set<Resumes> set=new HashSet<>(newResumesList);
            List<Resumes> list=new ArrayList<>();
            for(Resumes resumes3:resumes2){
                if(set.contains(resumes3)){
                    list.add(resumes3);
                }
            }
            return list;
        }else if(roleId.contains(103)){  //判断是否是招聘专员、面试官
            List<Resumes> resumesList=resumesMapper.selectResumesByRoleId(userId);
            List<Resumes> newResumesList=stringSub(resumesList);
            List<Resumes> resumes1=resumesMapper.selectResumesList(resumes);
            List<Resumes> resumes2=stringSub(resumes1);
            //两个集合取交集
            Set<Resumes> set=new HashSet<>(newResumesList);
            List<Resumes> list=new ArrayList<>();
            for(Resumes resumes3:resumes2){
                if(set.contains(resumes3)){
                    list.add(resumes3);
                }
            }
            return list;
        }
        List<Resumes> resumes1=resumesMapper.selectResumesList(resumes);
        List<Resumes> resumes2=stringSub(resumes1);
        return resumes2;
    }

    /**
     * 对教育背景和工作经验进行字符串分割
     * @param resumesList
     * @return
     */

    public static List<Resumes> stringSub(List<Resumes> resumesList){
        //创建一个新的返回对象，用来存放分割后的字符串
        List<Resumes> newResumesList=new ArrayList<>();
        for(Resumes res:resumesList){
            Resumes newRes=new Resumes();
            //进行字符串切割
            String education1=res.getEducation();
            String experience1=res.getExperience();
            String education2=education1.substring(education1.length()-2);
            String experience2=experience1.substring(experience1.length()-2);
            //封装对象
            newRes.setId(res.getId());
            newRes.setUserId(res.getUserId());
            newRes.setNickName(res.getNickName());
            newRes.setEducation(education2);
            newRes.setExperience(experience2);
            newRes.setSkills(res.getSkills());
            newRes.setCreateTime(res.getCreateTime());
            newRes.setUpdateTime(res.getUpdateTime());

            newResumesList.add(newRes);
        }
        return newResumesList;
    }
    /**
     * 新增简历管理
     *
     * @param resumes 简历管理
     * @return 结果
     */
    @Override
    public int insertResumes(Resumes resumes)
    {
        resumes.setCreateTime(DateUtils.getNowDate());
        resumes.setNickName(SecurityUtils.getUsername());
        resumes.setUserId(SecurityUtils.getUserId());
        return resumesMapper.insertResumes(resumes);
    }

    /**
     * 修改简历管理
     *
     * @param resumes 简历管理
     * @return 结果
     */
    @Override
    public int updateResumes(Resumes resumes)
    {
        resumes.setUpdateTime(DateUtils.getNowDate());
        return resumesMapper.updateResumes(resumes);
    }

    /**
     * 批量删除简历管理
     *
     * @param ids 需要删除的简历管理主键
     * @return 结果
     */
    @Override
    public int deleteResumesByIds(Long[] ids)
    {
        return resumesMapper.deleteResumesByIds(ids);
    }

    /**
     * 删除简历管理信息
     *
     * @param id 简历管理主键
     * @return 结果
     */
    @Override
    public int deleteResumesById(Long id)
    {
        return resumesMapper.deleteResumesById(id);
    }

    /**
     * 简历模板下载
     * @return
     */
   /* @Override
    public byte[] downloads() throws IOException, TemplateException {
        // Configuration实例化
        Configuration configuration = new Configuration();
        //设置编码
        configuration.setDefaultEncoding("UTF-8");
        //导入模板的信息
        Map map =new HashMap<>();
        //模板路径
        String path="D:/template/";
        String templateName = "template.ftl";
        //获取模板
        configuration.setClassForTemplateLoading(ResumesServiceImpl.class, "/");
        Template templatehtml = configuration.getTemplate(templateName);
        //创建文件，修改文件后缀名
        File file = new File(path+templateName.replace(".ftl", ".doc"));
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        //输出流，将问文档从ftl转换成doc
        // 指定编码表需使用转换流，转换流对象要接收一个字节输出流
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
        //将文件内容输出到doc文件
        templatehtml.process(map,out);
        //关闭输出流
        out.flush();
        return fileToBytes(file);
    }

    public static byte[] fileToBytes(File file) throws IOException {
        if (null == file) {
            return null;
        }
        byte[] buffer = null;
        FileInputStream fis = null;
        ByteArrayOutputStream bos = null;
        try {
            fis = new FileInputStream(file);
            bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n = -1;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            buffer = bos.toByteArray();
        } catch (IOException ex) {
            throw ex;
        } finally {
            try {
                if (null != bos) {
                    bos.close();
                }
            } catch (IOException ex) {
                throw ex;
            } finally {
                try {
                    if (null != fis) {
                        fis.close();
                    }
                } catch (IOException ex) {
                    throw ex;
                }
            }
        }
        return buffer;
    }*/
}
